/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.sbbs.jsf.car.dao;

import java.util.List;
import javax.annotation.PostConstruct;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.sbbs.genericdao.dao.jpa.GenericDAOImpl;
import org.sbbs.genericdao.search.MetadataUtil;
import org.sbbs.genericdao.search.jpa.JPAAnnotationMetadataUtil;
import org.sbbs.genericdao.search.jpa.JPASearchProcessor;
import org.sbbs.jsf.car.entity.Car;

/**
 *
 * @author Mr.fdc <fdc_messenger@126.com>
 */
//@Named("carDao")
public class CarDaoImpl extends GenericDAOImpl<Car, String> implements CarDao {

    protected final Logger logger = LogManager.getLogger(this.getClass().getName());
//    @PersistenceContext(unitName = "CARPU")
    private EntityManager em;

    @PostConstruct
    protected void init() {
        this.em = EmFactory.getEm();
        super.setEntityManager(em);

//        MetadataUtil metadataUtil = null;
//        try {
//        SessionImpl imp = em.unwrap(SessionImpl.class);
//        SessionFactory sf = imp.getSessionFactory();
//            EntityManagerFactoryImpl emf = (EntityManagerFactoryImpl) em.getEntityManagerFactory();
//            metadataUtil = getInstanceForSessionFactory(sf);
//        metadataUtil = HibernateJpaMetadataUtil.getInstanceForEntityManagerFactory(((SessionImpl) this.em.unwrap(SessionImpl.class)).getSessionFactory());
        MetadataUtil hmt = new JPAAnnotationMetadataUtil();
//        } catch (Exception e) {
//            
//            metadataUtil = (MetadataUtil) getInstanceForEntityManagerFactory((org.hibernate.ejb.HibernateEntityManagerFactory) em.getEntityManagerFactory());
//        }
        JPASearchProcessor searchProcessor = new JPASearchProcessor(hmt);
        super.setSearchProcessor(searchProcessor);
    }

    @Override
    public Car addCar(Car car) {
        return this.save(car);
    }

    @Override
    public boolean delCar(Car car) {
        return this.remove(car);
    }

    @Override
    public List<Car> findAllCar() {
        return this.findAll();
    }

    public EntityManager getEm() {
        return em;
    }

    public void setEm(EntityManager em) {
        this.em = em;
    }

}
